package com.dmcb.trade.business.crawlers.article;

import com.dmcb.trade.business.constants.CrawlConstant;
import com.dmcb.trade.business.entities.Article;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;

/**
 * 毕友网文章解析
 * Created by Administrator on 2016/7/14.
 */
@Service
public class BiYouParser  extends BaseParser {

    /**
     * 网站URL网址
     *
     * @return 链接
     */
    @Override
    protected String url() {
        return CrawlConstant.BIYOU_URL;
    }

    /**
     * 获取标题选择器
     *
     * @return 标题选择器
     */
    @Override
    protected String titleSelect() {
        return "div.article_title h1";
    }

    /**
     * 获取日期选择器
     *
     * @return 发布日期选择器
     */
    @Override
    protected String dateSelect() {
        return "div.article_info>span";
    }

    /**
     * 获取正文内容选择器
     *
     * @return 正文内容选择器
     */
    @Override
    protected String bodySelect() {
        return "div.article_content";
    }

    /**
     * 获取封面选择器
     *
     * @return 封面选择器
     */
    @Override
    protected String coverSelect() {
        return "div.article_content>p img[src]";
    }

    /**
     * 获取作者选择器
     *
     * @return 作者选择器
     */
    @Override
    protected String authorNameSelect() {
        return "div.article_info>span";
    }

    @Override
    protected void setAuthorName(Document doc, Article article) {
        Element element = doc.select(authorNameSelect()).get(1);
        String authorName = text(element);
        if (StringUtils.isNotBlank(authorName)) {
            authorName = authorName.trim().replace("编辑", "").replace("作者", "").replace("：", "").replace(":", "");

            article.setAuthorName(authorName);
        }
    }

    @Override
    protected String platform() {
        return "毕友网";
    }
}

